//文件配置
//author: Xavier

package config

import (
	"os"

	"gopkg.in/yaml.v2"
)

// config 总配置结构体
type config struct {
	Server       server       `yaml:"server"`
	Db           db           `yaml:"db"`
	Redis        redis        `yaml:"redis"`
	ImageSetting imageSetting `yaml:"imageSetting"`
	Log          log          `yaml:"log"`
}

// server 服务配置结构体
type server struct {
	Address string `yaml:"address"`
	Model   string `yaml:"model"`
}

// 数据库配置结构体
type db struct {
	Dialects string `yaml:"dialects"`
	Host     string `yaml:"host"`
	Port     int    `yaml:"port"`
	DB       string `yaml:"db"`
	Username string `yaml:"username"`
	Password string `yaml:"password"`
	Charset  string `yaml:"charset"`
	MaxIdle  int    `yaml:"maxIdle"`
	MaxOpen  int    `yaml:"maxOpen"`
}

// Redis配置结构体
type redis struct {
	Address  string `yaml:"address"`
	Password string `yaml:"password"`
}

// imageSetting 图片配置结构体
type imageSetting struct {
	UploadDir  string `yaml:"uploadDir"`
	UploadHost string `yaml:"uploadHost"`
}

// log 日志配置结构体
type log struct {
	Path  string `yaml:"path"`
	Name  string `yaml:"name"`
	Model string `yaml:"model"`
}

var Config *config

// init 初始化配置文件
func init() {
	yamlFile, err := os.ReadFile("./config.yaml")
	//错误处理
	if err != nil {
		panic(err)
	}
	//解析yaml文件
	err = yaml.Unmarshal(yamlFile, &Config)
	//错误处理
	if err != nil {
		panic(err)
	}
}
